<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use app\api\controller\Esapi;
use app\api\controller\KbxOther;
use think\db\Expression;

class Main extends \think\Controller
{
    public function index_old()
    {
        //tp版本号
        $info['tp'] = THINK_VERSION;
        //php版本
        $info['php'] = PHP_VERSION;
        //操作系统
        $info['win'] = PHP_OS;
        //最大上传限制
        $info['upload_size'] = ini_get('upload_max_filesize');
        //脚本执行时间限制
        $info['execution_time'] = ini_get('max_execution_time').'S';
        //环境
        $sapi = php_sapi_name();
        if($sapi = 'apache2handler') {
        	$info['environment'] = 'apache';
        } elseif($sapi = 'cgi-fcgi') {
        	$info['environment'] = 'cgi';
        } else {
        	$info['environment'] = 'cli';
        }
        //剩余空间大小
        //$info['disk'] = round(disk_free_space("/")/1024/1024,1).'M';
        $this->assign('info',$info);


        /**
         *网站信息
         */
        $web['user_num'] = Db::name('admin')->count();
        $web['admin_cate'] = Db::name('admin_cate')->count();
        $ip_ban = Db::name('webconfig')->value('black_ip');
        $web['ip_ban'] = empty($ip_ban) ? 0 : count(explode(',',$ip_ban));

        $web['article_num'] = Db::name('article')->count();
        $web['status_article'] = Db::name('article')->where('status',0)->count();
        $web['top_article'] = Db::name('article')->where('is_top',1)->count();
        $web['file_num'] = Db::name('attachment')->count();
        $web['status_file'] = Db::name('attachment')->where('status',0)->count();
        $web['ref_file'] = Db::name('attachment')->where('status',-1)->count();
        $web['message_num'] = Db::name('messages')->count();
        $web['look_message'] = Db::name('messages')->where('is_look',0)->count();

        if(Cookie::has('remember')) {
            $web['remember'] = Cookie::get('remember');
            //return $web['remember'];
        } else {
            $web['remember'] = '';
        }

        $this->assign('web',$web);

        return $this->fetch();
    }

    public function index(){
        $post = $this->request->post();
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $Esapi = new Esapi();
            $resData = $Esapi->esSearch($post['keywords']);

            $goodsData = [];
            if(!empty($resData['goods'])){//商品
                $orderTypeArr = array(
                    'Ticket_FIT'=>'地接社标品',
                    'Ticket_POI'=>'门票类产品',
                    'Hotel'=>'酒店',
                    'Custom'=>'定制产品',
                    'Vehicle'=>'用车',
                    'Ticket_AIR'=>'机票',
                    'Rental_Device'=>'物品购买/租赁',
                    'Other'=>'其它',
                    'Price_Gap'=>'补差价',
                    'Self_Drive'=>'自驾游',
                    'Personal_Tailor'=>'私人定制服务',
                    'Subscription'=>'代订服务',
                );
                $goodsIdArr = [];
                $goodsEsData = [];
                foreach ($resData['goods'] as $key=>$value) {
                    if(!empty($value['source']['id'])){
                        $goodsIdArr[] = $value['source']['id'];
                        $goodsEsData[$value['source']['id']] = $value;
                    }
                }
                $goods_where['goods_id'] = ["in",$goodsIdArr];
                $goods = db("goods")
                    ->where($goods_where)
                    ->field("goods_id,orderType,goods_image,goods_status,goods_tcode,show,up_url")
                    ->order(new Expression('field (goods_id,'. implode(',',$goodsIdArr).')'))
                    ->select();

                foreach ($goods as $key=>$value) {
                    //查询是否存在FAQ
                    $faq_flag = 0;
                    $faq = db("faq")
                        ->where("groupid",$value['goods_id'])
                        ->where("typeid",3)
                        ->where("deleted",0)
                        ->find();
                    if(!empty($faq)){
                        $faq_flag = 1;
                    }

                    $goodsData[] = [
                        'id'=>$value['goods_id'],
                        'show'=>$value['show'],
                        'goods_image'=>$value['goods_image'],
                        'goods_tcode'=>$value['goods_tcode'],
                        'faq_flag'=>$faq_flag,
                        'orderType'=>!empty($orderTypeArr[$value['orderType']]) ? $orderTypeArr[$value['orderType']] : '',
                        'goods_status'=>$value['goods_status'],
                        'name'=>!empty($goodsEsData[$value['goods_id']]['light']['name'][0]) ? $goodsEsData[$value['goods_id']]['light']['name'][0] : $goodsEsData[$value['goods_id']]['source']['name'],
                        'code'=>!empty($goodsEsData[$value['goods_id']]['light']['code'][0]) ? $goodsEsData[$value['goods_id']]['light']['code'][0] : $goodsEsData[$value['goods_id']]['source']['code'],
                        'city'=>!empty($goodsEsData[$value['goods_id']]['light']['city'][0]) ? $goodsEsData[$value['goods_id']]['light']['city'][0] : $goodsEsData[$value['goods_id']]['source']['city'],
                    ];
                }
            }
            $tripData = [];
            if(!empty($resData['trip'])){//定制行程
                $tripIdArr = [];
                $tripEsData = [];
                foreach ($resData['trip'] as $key=>$value) {
                    if(!empty($value['source']['id'])){
                        $tripIdArr[] = $value['source']['id'];
                        $tripEsData[$value['source']['id']] = $value;
                    }
                }
                $trip_where['t.id'] = ["in",$tripIdArr];
                $trip = db("trip")
                    ->alias("t")
                    ->join("admin a","t.adminid=a.id","left")
                    ->where($trip_where)
                    ->field("t.id,t.days,a.nickname as admin")
                    ->order(new Expression('field (t.id,'. implode(',',$tripIdArr).')'))
                    ->select();
                foreach ($trip as $key=>$value) {
                    if(!empty($tripEsData[$value['id']]['light']['name'][0])){
                        $name = $tripEsData[$value['id']]['light']['name'][0];
                    }else{
                        $name = $tripEsData[$value['id']]['source']['name'];
                        $name = substr($name,0,50);
                        $name = $name."……";
                    }

                    $tripData[] = [
                        'id'=>$value['id'],
                        'days'=>$value['days'],
                        'admin'=>$value['admin'],
                        'name'=>$name,
                    ];
                }
            }
            $productData = [];
            if(!empty($resData['product'])){//产品
                //供应商   不能放上面，引入类构造方法会过滤html标签
                list($supplier,$supplierNameArr) = (new Product())->getSupplierList();

                $productIdArr = [];
                $productEsData = [];
                foreach ($resData['product'] as $key=>$value) {
                    if(!empty($value['source']['id'])){
                        $productIdArr[] = $value['source']['id'];
                        $productEsData[$value['source']['id']] = $value;
                    }
                }

                $product_where['p.id'] = ["in",$productIdArr];
                $product = db("product")
                    ->alias("p")
                    ->join("admin a","p.adminid=a.id","left")
                    ->where($product_where)
                    ->field("p.id,p.days,p.up_url,p.supplier_id,p.supplier_item_code,a.nickname as admin")
                    ->order(new Expression('field (p.id,'. implode(',',$productIdArr).')'))
                    ->select();
                foreach ($product as $key=>$value) {
                    if(!empty($value['supplier_id'])){
                        if($value['supplier_id']=='999999'){
                            $supplier_name = '自营';
                        }elseif($value['supplier_id']=='77777'){
                            $supplier_name = '无';
                        }elseif($value['supplier_id']=='1000000'){
                            $supplier_name = '其他';
                        }else{
                            $supplier_name = !empty($supplierNameArr[$value['supplier_id']]) ? $supplierNameArr[$value['supplier_id']] : '';
                        }
                    }else{
                        $supplier_name = '';
                    }

                    //查询是否存在FAQ
                    $faq_flag = 0;
                    $faq = db("faq")
                        ->where("groupid",$value['id'])
                        ->where("typeid",1)
                        ->where("deleted",0)
                        ->find();
                    if(!empty($faq)){
                        $faq_flag = 1;
                    }

                    $productData[] = [
                        'id'=>$value['id'],
                        'days'=>$value['days'],
                        'admin'=>$value['admin'],
                        'up_url'=>$value['up_url'],
                        'supplier_name'=>$supplier_name,
                        'faq_flag'=>$faq_flag,
                        'supplier_item_code'=>$value['supplier_item_code'],
                        'name'=>!empty($productEsData[$value['id']]['light']['name'][0]) ? $productEsData[$value['id']]['light']['name'][0] : $productEsData[$value['id']]['source']['name'],
                        'code'=>!empty($productEsData[$value['id']]['light']['item_num'][0]) ? $productEsData[$value['id']]['light']['item_num'][0] : $productEsData[$value['id']]['source']['item_num'],
                    ];
                }
            }
            $poiData = [];
            if(!empty($resData['poi'])){//POI
                foreach ($resData['poi'] as $key=>$value) {
                    $poiData[] = [
                        'id'=>$value['source']['id'],
                        'name'=>!empty($value['light']['name'][0]) ? $value['light']['name'][0] : $value['source']['name'],
                        'ticket'=>!empty($value['light']['ticket'][0]) ? $value['light']['ticket'][0] : $value['source']['ticket'],
                        'city'=>!empty($value['light']['city'][0]) ? $value['light']['city'][0] : $value['source']['city'],
                    ];
                }
            }
            $faqData = [];
            if(!empty($resData['faq'])){//FAQ
                foreach ($resData['faq'] as $key=>$value) {
                    if(!empty($value['light']['content'][0])){
                        $content = $value['light']['content'][0];
                    }else{
                        $content = $value['source']['content'];
                        if(strlen($content)>50){
                            $content = substr($content,0,50);
                            $content = $content."……";
                        }
                    }
                    $faqData[] = [
                        'id'=>$value['source']['id'],
                        'typeid'=>$value['source']['typeid'],
                        'typename'=>$value['source']['typename'],
                        'groupid'=>$value['source']['groupid'],
                        'name'=>!empty($value['light']['name'][0]) ? $value['light']['name'][0] : $value['source']['name'],
                        'content'=>$content,
                    ];
                }
            }

            $this->assign('goodsData', $goodsData);
            $this->assign('tripData', $tripData);
            $this->assign('productData', $productData);
            $this->assign('poiData', $poiData);
            $this->assign('faqData', $faqData);
            $this->assign('wherelist', $post);
        }

        return $this->fetch();
    }



}
